﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CTRL;
using DAL;
using System.Threading;

namespace GUI_Administration
{
    public partial class GUIBatteryManufacturer : Form
    {
        private static GUIBatteryManufacturer instance;
        private CtrlBatteryManufacturer ctrlBatMan;

        private GUIBatteryManufacturer()
        {
            InitializeComponent();

            ctrlBatMan = CtrlBatteryManufacturer.Instance;

            UpdateList();
        }

        public static GUIBatteryManufacturer Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUIBatteryManufacturer();

                return instance;
            }
        }

        private void UpdateList()
        {
            dataGridView.Rows.Clear();

            List<BatteryManufacturer> listMan = ctrlBatMan.GetAllBatteryManufacturers().ToList();

            foreach (BatteryManufacturer m in listMan)
                dataGridView.Rows.Add(m.manufacturerID, m.name);
        }

        private void newManBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMan.CreateBatteryManufacturer(dataGridView.CurrentRow.Cells[1].Value.ToString());
            UpdateList();
        }

        private void GetManBtn_Click(object sender, EventArgs e)
        {
            if (int.Parse(IDtxtBox.Text) == 0)
                UpdateList();
            else
            {
                dataGridView.Rows.Clear();
                BatteryManufacturer manuf = ctrlBatMan.GetBatteryManufacturer(int.Parse(IDtxtBox.Text));
                dataGridView.Rows.Add(manuf.manufacturerID, manuf.name);
            }
        }

        private void UpdateManBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMan.UpdateBatteryManufacturer(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()),
                dataGridView.CurrentRow.Cells[1].Value.ToString());
            UpdateList();
        }

        private void DeleteManBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMan.DeleteBatteryManufacturer(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()));
            UpdateList();
        }

        private void GetManByNameBtn_Click(object sender, EventArgs e)
        {
            if (nameTxtBox.Text.ToString() == "0")
                UpdateList();
            else
            {
                dataGridView.Rows.Clear();
                BatteryManufacturer manuf = ctrlBatMan.GetBatteryManufacturerByName(nameTxtBox.Text.ToString());
                dataGridView.Rows.Add(manuf.manufacturerID, manuf.name);
            }
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}